package com.study.proxy.dynamicproxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkMeipo implements InvocationHandler {

    private IPerson person;

    public IPerson newInstance(IPerson person){
        this.person = person;
        Class clazz = person.getClass();

        return (IPerson) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object obj = method.invoke(this.person,args);
        after();
        return obj;
    }

    private void after() {
        System.out.println("after doing...");
    }

    private void before() {
        System.out.println("before doing...");
    }
}
